﻿<cfsilent>
	<cfscript>
		
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");

		taskProp = getProperty("serviceFactory").getBean("taskPropertyAdvice");
		
		response = structNew();
		structInsert(response, "success", false, true);

		termId = event.getArg("TID");
		courseId = event.getArg("CID");
		
		transaction {
			
			/* 载入课程排课任务 */
			sql = "	SELECT
						a.task_id,a.task_type
					FROM
						t_task_arrange a
					WHERE
						a.cid = :courseId 
						AND
						a.term_id = :termId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "courseId", value = courseId, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "termId", value = termId, cfsqltype = "cf_sql_char");
			rs_taskArrange = queryObj.execute(sql = sql).getResult();
			
			for(a = 1; a lte rs_taskArrange.recordCount; a++){
				
				taskId = rs_taskArrange['task_id'][a];
				taskType = rs_taskArrange['task_type'][a];
				
				/* 载入教学任务 */
				switch(taskType){
					case 'C':
						sql = "SELECT
							tsk.tsk_id,
							tsk.task_prop
						FROM
							t_common_task tsk
						WHERE
							 tsk.tsk_id = :taskId ";
						break;
					case 'P':
						sql = "SELECT
							tsk.tsk_id,
							tsk.task_prop
						FROM
							t_task tsk
						WHERE
							 tsk.tsk_id = :taskId ";
						break;
					case 'R':
						sql = "SELECT
							tsk.tsk_id,
							tsk.task_prop
						FROM
							t_reopen_task tsk
						WHERE
							 tsk.tsk_id = :taskId ";
						break;
					case 'T':
						sql = "SELECT
							tsk.tsk_id,
							tsk.task_prop
						FROM
							t_sport_task tsk
						WHERE
							 tsk.tsk_id = :taskId ";
						break;
				}
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				rs_taskEntity = queryObj.execute(sql = sql).getResult();

				/* 载入排课任务 */
				
				/* 删除教学班时间占用情况表 */ 
				sql = "	DELETE FROM
							t_task_timetable ttt
						WHERE
							ttt.task_id = :taskId ";
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				queryObj.execute(sql = sql);

				/* 删除地点时间占用表 */
				sql = "	DELETE FROM
							t_clsroom_timetable ttt
						WHERE
							ttt.crtime_id IN(
								SELECT
									tf.crtime_id
								FROM
									t_clsroom_forcourse tf
								WHERE
									tf.task_id = :taskId 
							)";

				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				queryObj.execute(sql = sql);

				/* 删除教师时间占用表 */
				sql = "	DELETE FROM
							t_teacher_timetable ttt
						WHERE
							ttt.task_id = :taskId ";
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				queryObj.execute(sql = sql);

				/* 解锁排课任务 */
				taskProp.parseProperty( rs_taskEntity.task_prop );
				taskProp.setArrangeDone( false );
					
				/* 清除排课时间 地点 */
				
				/* 重置教学班最大容量 */
				
				switch(taskType){
					case 'C':
						sql = "	UPDATE
									t_common_task t
								SET
									t.task_prop = :taskProp ,
									t.task_time = '' ,
									t.task_place = '' ,
									t.task_capacity = 0
								WHERE
									t.tsk_id = :taskId ";
						break;
					case 'P':
						sql = "	UPDATE
									t_task t
								SET
									t.task_prop = :taskProp ,
									t.task_time = '' ,
									t.task_place = '' ,
									t.task_capacity = 0
								WHERE
									t.tsk_id = :taskId ";
						break;
					case 'R':
						sql = "	UPDATE
									t_reopen_task t
								SET
									t.task_prop = :taskProp ,
									t.task_time = '' ,
									t.task_place = '' ,
									t.task_capacity = 0
								WHERE
									t.tsk_id = :taskId ";
						break;
					case 'T':
						sql = "	UPDATE
									t_sport_task t
								SET
									t.task_prop = :taskProp ,
									t.task_time = '' ,
									t.task_place = '' ,
									t.task_capacity = 0
								WHERE
									t.tsk_id = :taskId ";
						break;
				}
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				queryObj.addParam(name ="taskProp", value=taskProp.stringProperty(), cfsqltype="cf_sql_varchar");
				queryObj.execute(sql = sql);
				
				/* 清除已排周学时 */
				sql = "	UPDATE
							t_task_arrange t
						SET
							t.period_done = 0
						WHERE
							t.task_id = :taskId ";
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "taskId", value = taskId, cfsqltype="cf_sql_varchar");
				queryObj.execute(sql = sql);
				
				/* 载入调停课信息 */
				sql = "	DELETE FROM
							t_task_adjust t
						WHERE
							t.task_id = :taskId ";
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "taskId", value = taskId, cfsqltype="cf_sql_varchar");
				queryObj.execute(sql = sql);
				
			}
			
			structInsert(response, "success", true, true);
		}
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>